package com.whfc.hj.client;

import com.whfc.common.util.DateUtil;
import com.whfc.hj.entity.HjMsg;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2020-12-08 15:41
 */
public class HjClientHandler extends ChannelInboundHandlerAdapter {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        logger.info("==============channel--register==============");
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        logger.info("==============channel--unregistered==============");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.info("==============channel--inactive==============");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        logger.info("==============channel--active==============");

        HjMsg msg = this.createHjMsg2011();

        ctx.channel().writeAndFlush(msg);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
        logger.info("==============channel--read==============");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        logger.error("==============exceptionCaught==============", cause);
        ctx.close();
    }

    /**
     * 创建测试数据
     *
     * @return
     */
    private HjMsg createHjMsg2011() {
        Date now = new Date();
        HjMsg msg = new HjMsg();
        msg.setQn(DateUtil.formatDate(now, "yyyyMMddHHmmssSSS"));
        msg.setSt("22");
        msg.setCn("2011");
        msg.setMn("Y029000JH07609");
        msg.setPw("123456");
        msg.setFlag(5);
        msg.setDataTime(DateUtil.formatDate(now, "yyyyMMddHHmmss"));
        msg.setLaRtd("56.8");
        msg.setLaFlag("N");
        msg.setA01001Rtd("14.9");
        msg.setA01001Flag("N");
        msg.setA01002Rtd("81.1");
        msg.setA01002Flag("N");
        msg.setA01006Rtd("97.16");
        msg.setA01006Flag("N");
        msg.setA01007Rtd("0.8");
        msg.setA01007Flag("N");
        msg.setA01008Rtd("112");
        msg.setA01008Flag("N");
        msg.setA34001Rtd("96.8");
        msg.setA34001Flag("N");
        msg.setA34002Rtd("81.0");
        msg.setA34002Flag("N");
        msg.setA34004Rtd("46.4");
        msg.setA34004Flag("N");
        return msg;
    }
}
